package com.bwei.mypaot.base.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.mypaot.R;
import com.bwei.mypaot.base.activity.StartActivity;
import com.bwei.mypaot.home.DanActivity;
import com.bwei.mypaot.view.MainLinearLayout;
import com.bwei.mypaot.view.PickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.umeng.socialize.utils.DeviceConfig.context;

/**
 * Created by Administrator on 2017/2/14.
 */

public class Fragment_home extends BaseFragment {

    private View view;
    private RelativeLayout rl_start;
    private RelativeLayout rl_end;
    private RelativeLayout rl_time;
    private TextView address_content;
    private TextView address_endcontent;
    private List<String> data;
    private List<String> seconds;
    private PickerView minute_pv;
    private PickerView second_pv;
    private Button bt_native;
    private Button bt_positive;
    private TextView time_get;
    private String timehour;
    private String timeminute;
    private String format;
    private Button bt_downdan;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getActivity(), R.layout.fragment_home,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initHeader();
        initWidget();
        setWidgetState();
    }

    @Override
    public void initHeader() {

    }

    @Override
    public void initWidget() {
        rl_start = (RelativeLayout) view.findViewById(R.id.rl_start);
        rl_end = (RelativeLayout) view.findViewById(R.id.rl_end);
        rl_time = (RelativeLayout) view.findViewById(R.id.rl_time);
        address_content = (TextView) view.findViewById(R.id.address_content);
        address_endcontent = (TextView) view.findViewById(R.id.address_endcontent);
        time_get = (TextView) view.findViewById(R.id.time_get);
        bt_downdan = (Button) view.findViewById(R.id.bt_downdan);




    }

    @Override
    public void setWidgetState() {
        rl_start.setOnClickListener(this);
        rl_end.setOnClickListener(this);
        rl_time.setOnClickListener(this);
        bt_downdan.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rl_start:
                Intent intent=new Intent(getActivity(), StartActivity.class);
                startActivityForResult(intent,1);


                break;
            case R.id.rl_end:

                Intent intentend=new Intent(getActivity(), StartActivity.class);
                startActivityForResult(intentend,2);

                break;
            case R.id.rl_time:

            View timeview= View.inflate(getActivity(),R.layout.time_activity,null);


                //分
                minute_pv = (PickerView) timeview.findViewById(R.id.minute_pv);
                //秒
                second_pv = (PickerView) timeview.findViewById(R.id.second_pv);
                bt_native = (Button) timeview.findViewById(R.id.bt_native);
                bt_positive = (Button) timeview.findViewById(R.id.bt_positive);

                data = new ArrayList<String>();
                data.add("今天");
                data.add("明天");
                data.add("后天");

                seconds = new ArrayList<String>();
                seconds.add("立即");

                //调用此方法可以添加数据并且获得选中的数据
                picker();

                final PopupWindow popWnd = new PopupWindow(getActivity());
                popWnd.setContentView(timeview);
                popWnd.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
                popWnd.setFocusable(true);
                popWnd.showAtLocation(view, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0);

                //为时间滚轮设置当前时间
//                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("kk时mm分");
//                format = simpleDateFormat.format(new Date());
//                Log.d("时间",format);
//
//                time_get.setText(timehour+"时"+timeminute+"分");
                time_get.setText("");
                bt_native.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popWnd.dismiss();
                    }
                });
                bt_positive.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null == timehour ||null==timeminute){
                            Toast.makeText(getActivity(),"时间不能为空",Toast.LENGTH_SHORT).show();

                        }else{
                            time_get.setText(timehour+"时"+timeminute+"分");
                            popWnd.dismiss();
                        }

                    }
                });
                break;
            case R.id.bt_downdan:
                Intent intentdan=new Intent(getActivity(), DanActivity.class);
                startActivity(intentdan);



                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        String address=data.getStringExtra("address");
        if (requestCode==1&&resultCode==2){
            //设置回传的开始地址
            address_content.setText(address);

        }else if(requestCode==resultCode){
            //设置回传的结束地址地址
            address_endcontent.setText(address);

        }


    }

    public void picker(){
        for (int i = 0; i <=23; i++)
        {

            data.add(i < 10 ? "0" + i : "" + i);
        }
        //循环获得秒数
        for (int i = 0; i < 60; i++)
        {
            seconds.add(i < 10 ? "0" + i : "" + i);
        }
        minute_pv.setData(data);
        minute_pv.setOnSelectListener(new PickerView.onSelectListener()
        {

            @Override
            public void onSelect(String text)
            {
                if (null==text){
                    timehour="";
                }else{
                    timehour = text;
                }

//                Toast.makeText(getActivity(), "选择了 " + text + " 时",
//                        Toast.LENGTH_SHORT).show();
            }
        });
        second_pv.setData(seconds);
        second_pv.setOnSelectListener(new PickerView.onSelectListener()
        {

            @Override
            public void onSelect(String text)
            {
                if (null==text){
                    timeminute="";
                }else{
                    timeminute = text;
                }

//                Toast.makeText(getActivity(), "选择了 " + text + " 分",
//                        Toast.LENGTH_SHORT).show();
            }
        });
    }




    public boolean onCreateOptionsMenu(Menu menu)
    {
        getActivity().getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}
